<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Register extends MY_Controller
{

    public function index()
    {
        $data = array( );

        //注册表单
        $registerForm = Modules::run( 'registerForm' );
        $data['registerForm'] = $registerForm;

        $this->get_common_section( $data );
        $this->load->view( 'register', $data );
    }

    /**
     * 处理注册数据
     */
    public function submit()
    {
        if ( $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            //合法注册提交
            $userData = $this->_getRegUserData();
            if ( $userData )
            {
                //数据合法 并插入用户数据
                $userId = $this->model_user_add( $userData );
                //记录到session的数据
                $userSessionData = array(
                    'id' => $userId,
                    'username' => $userData['username'],
                    'email' => $userData['email'],
                    'groupid' => $userData['groupid'],
                    'groupname' => $userData['groupname'],
                );
                //记录到session中
                $this->session->setUserSession( $userSessionData );

                //提示注册成功
                $messageData = array(
                    'title' => '操作成功',
                    'message' => '欢迎成为会员，4秒后自动跳转回首页',
                    'autoRedirect' => true,
                    'autoRedirectTimeOut' => 4,
                    'autoRedirectURL' => config_item( 'base_url' ),
                );
                $messageBox = Modules::run( 'messageBox', $messageData );
                $data['registerForm'] = $messageBox;

                $this->get_common_section( $data );
                $this->load->view( 'register', $data );
            }
            else
            {
                $this->index();
            }
        }
        else
        {
            //显示表单
            $this->index();
        }
    }

    /**
     * 验证用户数据合法性
     * @return bool 是否合法
     */
    private function _getRegUserData()
    {
        $this->form_validation->set_message( 'required', '%s不能为空' );
        $this->form_validation->set_message( 'matches', '%s不正确' );
        $this->form_validation->set_message( 'min_length', '%s长度至少为6位' );
        $this->form_validation->set_message( 'valid_email', '%s格式不正确' );

        $this->form_validation->set_rules( 'username', '用户名', 'required|callback_checkUsername' );
        $this->form_validation->set_rules( 'password', '密码', 'required|min_length[6]' );
        $this->form_validation->set_rules( 'confirmpwd', '确认密码', 'required|matches[password]' );
        $this->form_validation->set_rules( 'email', '邮箱', 'required|valid_email|callback_checkEmail' );
        if ( $this->form_validation->run() )
        {
            //获取默认组数据
            $defaultGroup = $this->model_group_getDefault();
            $data = array(
                'username' => strip_tags( $this->input->post( 'username' ) ),
                'password' => md5( $this->input->post( 'password' ) ),
                'email' => $this->input->post( 'email' ),
                'groupid' => $defaultGroup['id'],
                'groupname' => $defaultGroup['name'],
            );
            return $data;
        }
        return false;
    }

    /**
     * 检查用户名是否被注册
     * @param string $userName 用户名
     */
    public function checkUsername( $userName )
    {
        $userData = $this->model_user_getByUserName( strip_tags( $userName ) );
        if ( $userData )
        {
            $this->form_validation->set_message( 'checkUsername', '该%s已经被注册，请换一个' );
            return false;
        }
        return true;
    }

    /**
     * 检查用户邮箱是否被注册
     * @param string $email 邮箱
     */
    public function checkEmail( $email )
    {
        $userData = $this->model_user_getByEmail( $email );
        if ( $userData )
        {
            $this->form_validation->set_message( 'checkEmail', '该%s已经注册，你确定你没有注册过吗' );
            return false;
        }
        return true;
    }

}